// 定义完整的Tree类型
export class Tree {
  id?: number
  label?: string
  file: string
  seq_path?: string
  path?:string
  children?: Tree[]
  flag?: string
  type?: string
  hidden?: boolean
  edit?: boolean // 补全缺失的edit属性

  constructor(file: string, children: Tree[]) {
    this.file = file
    this.children = children
  }
}

export class TransData {
  id: number
  label: string
  path: string
  constructor(id: number, label: string, path: string) {
    this.id = id
    this.label = label
    this.path = path
  }
}

// 查找bom中剩余prt数量
export const prtCount = (bom: Tree): number => {
  let count = 0

  function countRecusively(head: Tree) {
    if (head.type === 'prt' && !head.hidden) {
      count++
    }
    if (head.children) {
      head.children.forEach((child) => {
        countRecusively(child)
      })
    }
  }

  countRecusively(bom)
  return count
}

// 递归过滤节点，过滤掉所有隐藏节点
export const filterNodes = (nodes: Tree[]): Tree[] => {
  return nodes
    .filter((node) => !node.hidden)
    .map((node) => ({
      ...node,
      // 关键：子节点处理时也不复制，保持原始引用
      children: node.children ? filterNodes(node.children) : undefined,
    }))
}

// 递归查找节点
export const findNodeById = (treeData: Tree[], targetId: number): Tree | null => {
  for (const node of treeData) {
    if (node.id == targetId) return node
    if (node.children && node.children.length) {
      const found = findNodeById(node.children, targetId)
      if (found != null) return found
    }
  }
  return null
}

// 递归查找一个asm下面所有的尚未隐藏的prt
export const findUnhiddenPrtUnderAsm = (asm: Tree, prts: Tree[]) => {
  asm.children?.forEach((child) => {
    if (child.type == 'prt' && !child.hidden) {
      prts.push(child)
    }
    if (child.type == 'asm') {
      findUnhiddenPrtUnderAsm(child, prts)
    }
  })
}

// 递归收集需要修改的节点
export const findNodesNeedToBeChange = (result: Tree[], treeData: Tree[]): void => {
  for (const node of treeData) {
    if (node.hidden) result.push(node)
    if (node.children && node.children.length) {
      findNodesNeedToBeChange(result, node.children)
    }
  }
}

// 递归获取最大ID（修复原ID生成问题）
export const getMaxId = (treeData: Tree[]): number => {
  let maxId = 0
  const traverse = (nodes: Tree[]) => {
    nodes.forEach((node) => {
      if (node.id > maxId) maxId = node.id
      if (node.children && node.children.length) traverse(node.children)
    })
  }
  traverse(treeData)
  return maxId
}

export const packBom = (bom: Tree) => {
  let index = 0

  function packBomRecusively(ibom: Tree) {
    ibom.id = index++
    ibom.label = ibom.file.slice(0, ibom.file.lastIndexOf('.'))
    if (ibom.file.toLowerCase().endsWith('asm')) {
      ibom.type = 'asm'
    } else if (ibom.file.toLowerCase().endsWith('prt')) {
      ibom.label = ibom.file.slice(0, ibom.file.lastIndexOf('.'))
      ibom.type = 'prt'
      ibom.flag = 'entity'
      ibom.hidden = false
    }

    if (ibom.children) {
      ibom.children.forEach((child) => packBomRecusively(child))
    }
  }

  packBomRecusively(bom)
  return bom
}

// const dataSource2 = ref<Tree[]>([
//   {
//     id: 2000,
//     label: '惰轮轴总成',
//     type: 'asm',
//     children: [
//       {
//         id: 2001,
//         label: '惰轮轴',
//         children: [],
//         flag: 'entity',
//         type: 'prt',
//         hidden: false,
//       },
//       {
//         id: 2002,
//         label: '挡圈',
//         children: [],
//         flag: 'entity',
//         type: 'prt',
//         hidden: false,
//       },

//       {
//         id: 2003,
//         label: '轴承外圈',
//         children: [],
//         flag: 'entity',
//         type: 'prt',
//         hidden: false,
//       },
//       {
//         id: 2004,
//         label: '轴承内圈',
//         children: [],
//         flag: 'entity',
//         type: 'prt',
//         hidden: false,
//       },
//     ],
//   },
// ])
